package com.fan.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.codec.PercentCodec;
import cn.hutool.json.JSONUtil;
import com.fan.common.HttpStatus;
import com.fan.common.ResponseResult;
import com.fan.config.MinioConfig;
import com.fan.util.MinioManger;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.minio.messages.Bucket;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author D-BJ-144
 * @description MinioController
 * @Date 2024/10/30 16
 */
@Api(tags = "文件相关接口")
@RestController
@Slf4j
@RequestMapping("/minio")
public class MinioController {

    @Autowired
    private MinioManger minioManger;
    @Autowired
    private MinioConfig minioConfig;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "查看存储bucket是否存在")
    @GetMapping("/bucketExists")
    public ResponseResult bucketExists(@RequestParam("bucketName") String bucketName) {
        if (minioManger.bucketExists(bucketName)) {
            return ResponseResult.success("bucketName is exit!");
        } else
            return ResponseResult.error(HttpStatus.NOT_FOUND_ERROR, "bucketName is not exit!");
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "创建存储bucket")
    @GetMapping("/makeBucket")
    public ResponseResult makeBucket(String bucketName) {
        if (minioManger.makeBucket(bucketName)) {
            return ResponseResult.success("create bucket success!");
        } else
            return ResponseResult.error(HttpStatus.OPERATION_ERROR, "create bucket error!");

    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "删除存储bucket")
    @GetMapping("/removeBucket")
    public ResponseResult removeBucket(String bucketName) {
        if (minioManger.removeBucket(bucketName)) {
            return ResponseResult.success("removeBucket  success!");
        } else
            return ResponseResult.error(HttpStatus.OPERATION_ERROR, "removeBucket error!");
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "获取全部bucket")
    @GetMapping("/getAllBuckets")
    public ResponseResult getAllBuckets() throws JsonProcessingException {
        List<Bucket> allBuckets = minioManger.getAllBuckets();
        List<Map<String,Object>> list = new ArrayList<>();
        for (Bucket bucket : allBuckets) {
            Map<String, Object> map = new HashMap<>();
            map.put("name",bucket.name());
            list.add(map);
        }
        return ResponseResult.success(list);
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "文件上传返回url")
    @PostMapping("/upload")
    public ResponseResult upload(@RequestParam("file") MultipartFile file) {
        String objectName = minioManger.upload(file);
        if (null != objectName) {
            String url = (minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + objectName);
            return ResponseResult.success(url);
        }
        return ResponseResult.error(HttpStatus.OPERATION_ERROR, "upload error!");
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "图片/视频预览")
    @GetMapping("/preview")
    public ResponseResult preview(@RequestParam("fileName") String fileName) throws UnsupportedEncodingException {
        String preview = minioManger.preview(fileName);
        System.out.println(preview);
        return ResponseResult.success(URLEncoder.encode(Base64.encode(preview), "UTF-8"));
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "文件下载")
    @GetMapping("/download")
    public void download(@RequestParam("fileName") String fileName, HttpServletResponse res) {
        minioManger.download(fileName, res);
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除文件", notes = "根据url地址删除文件")
    @PostMapping("/delete")
    public ResponseResult remove(String url) {
        String objName = url.substring(url.lastIndexOf(minioConfig.getBucketName() + "/") + minioConfig.getBucketName().length() + 1);
        boolean remove = minioManger.remove(objName);
        if (remove) {
            return ResponseResult.success(objName + "delete success!");
        } else return ResponseResult.error(HttpStatus.OPERATION_ERROR, objName + "delete error!");
    }
}
